﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DBLClient.DBLToolService;
using DBLClient.DBLAdminConfigService;
namespace DBLClient.DBLForm
{
    public partial class AddAdminForm : Form
    {
        private string EditMode = string.Empty;
        private int id;
        public AddAdminForm(string editMode,int id)
        {
            InitializeComponent();
            this.EditMode = editMode;
            this.id = id;
            if (editMode.Equals("U"))
            {
                //把admin信息显示在界面上
                AdminConfigService admin = new AdminConfigService();
                DataSet ds = admin.GetAdminByID(id);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    textBoxAdminName.Text = ds.Tables[0].Rows[0]["adminname"].ToString();
                    textBoxAdminPassword.Text = ds.Tables[0].Rows[0]["adminpassword"].ToString();
                    string[] adminlevel = ds.Tables[0].Rows[0]["adminlevel"].ToString().Split(',');
                    listBoxAdminLevel.Items.AddRange(adminlevel);
                }
            }
        }

        private void buttonSearchLocation_Click(object sender, EventArgs e)
        {
            
            string searchWord = textBoxLocation.Text;

            ToolService tool = new ToolService();
            DataSet ds = tool.FindLocationByName(searchWord);

            dataGridView1.DataSource = ds.Tables[0];
            
        }

        private void buttonPutIn_Click(object sender, EventArgs e)
        {
            //放入权限列表
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请选中加入权限列表的值");
            }
            string code = dataGridView1.CurrentRow.Cells["columnCode"].Value.ToString();
            if (!listBoxAdminLevel.Items.Contains(code))
            {
                listBoxAdminLevel.Items.Add(code);
            }
        }

        private void buttonPutOut_Click(object sender, EventArgs e)
        {
            //拿出权限
            int index = listBoxAdminLevel.SelectedIndex;
            if (index != -1)
            {
                listBoxAdminLevel.Items.RemoveAt(index);
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            //保存
            //收集信息
            string adminname = textBoxAdminName.Text.ToString();
            string password = textBoxAdminPassword.Text.ToString();
            string adminlevel = string.Empty;
            foreach (object o in listBoxAdminLevel.Items)
            {
                adminlevel = adminlevel+ o.ToString()+",";

            }
            adminlevel = adminlevel.TrimEnd(',');

            AdminConfigService admin = new AdminConfigService();

            if (EditMode.Equals("U"))
            {
                admin.UpdateAdmin(id, adminname, password, adminlevel);
            }
            else if (EditMode.Equals("A"))
            {
                admin.AddAdmin(adminname, password, adminlevel);
            }
        }

        private void buttonAll_Click(object sender, EventArgs e)
        {
            listBoxAdminLevel.Items.Clear();
            listBoxAdminLevel.Items.Add("ALL");
        }

        private void buttonSuperAdmin_Click(object sender, EventArgs e)
        {
            listBoxAdminLevel.Items.Clear();
            listBoxAdminLevel.Items.Add("ADMIN");
        }
    }
}
